<?php
/*
 * 
 * Indrajith FTP Manager v.1.0
 *
 * Coded by AJITH KP [ http://www.facebook.com/ajithkp560 ] & VISHNU NATH KP[ http://www.facebook.com/vishnunathkp ]
 * Greets to INDISHELL, TOF, ICH, etc
 * Greets to AMSTECK ARTS & SCIENCE COLLEGE 3'rd SEMESTER BCA STUDENTS -> Dheeraj, Ashwin, Ragesh Patteri, Vijay, Sishil, Vipin, Sharath, Anuprasad, Arjun
 * Greets to D@RKW@LF, ASHELL, CODED32, ABDUL GHAFFAR, and ALL OTHER FRIENDS
 *
 * */
?>



<html>
<head>
	<!-- http://www.terminalcoders.blogspot.in -->
<style>
	
    html { background:url() black; }
    #loginbox { font-size:11px; color:green; width:1200px; height:220px; border:1px solid #4C83AF; background-color:#111111; border-radius:5px; -moz-boder-radius:5px; position:relative; top:250px; }
    input { font-size:11px; background:#191919; color:green; margin:0 4px; border:1px solid #222222; }
    loginbox td { border-radius:5px; font-size:11px; }
    .header { size:25px; color:green; }
    h1 { font-family:DigifaceWide; color:green; font-size:200%; }
    h1:hover { text-shadow:0 0 20px #00FFFF, 0 0 100px #00FFFF; }
    .go { height: 50px; width: 50px;float: left; margin-right: 10px; display: none; background-color: #090;}
    .input_big { width:75px; height:30px; background:#191919; color:green; margin:0 4px; border:1px solid #222222; font-size:17px; }
    hr { border:1px solid #222222; }
    #meunlist { width: auto; height: auto; font-size: 12px; font-weight: bold; }
    #meunlist ul { padding-top: 5px; padding-right: 5px; padding-bottom: 7px; padding-left: 2px; text-align:center; list-style-type: none; margin: 0px; }
    #meunlist li { margin: 0px; padding: 0px; display: inline; }
    #meunlist a { font-size: 14px; text-decoration:none; font-weight: bold;color:green;clear: both;width: 100px;margin-right: -6px; padding-top: 3px; padding-right: 15px; padding-bottom: 3px; padding-left: 15px; }
    #meunlist a:hover { background: #333; color:green; }
    .menubar {-moz-border-radius: 10px; border-radius: 10px; border:1px solid green; padding:4px 8px; line-height:16px; background:#111111; color:#aaa; margin:0 0 8px 0;  }
    .menu { font-size:25px; color: }
    .textarea_edit { background-color:#111111; border:1px groove #333; color:green; }
    .textarea_edit:hover { text-decoration:none; border:1px dashed #333; }
    .input_butt {font-size:11px; background:#191919; color:#4C83AF; margin:0 4px; border:1px solid #222222;}
    #result{ -moz-border-radius: 10px; border-radius: 10px; border:1px solid green; padding:4px 8px; line-height:16px; background:#111111; color:#aaa; margin:0 0 8px 0; min-height:100px;}
    .table{ width:100%; padding:4px 0; color:#888; font-size:15px; }
    #resultx{ -moz-border-radius: 10px; border-radius: 10px; border:1px solid green; padding:4px 8px; line-height:16px; background:#111111; color:#aaa; margin:0 0 8px 0; min-height:50px;}
    .table a{ text-decoration:none; color:green; font-size:15px; }
    .table a:hover{text-decoration:underline;}
    .table td{ border-bottom:1px solid #222222; padding:0 8px; line-height:24px; vertical-align:top; }
    .table th{ padding:3px 8px; font-weight:normal; background:#222222; color:#555; }
    .table tr:hover{ background:#181818; }
    .tbl{ width:100%; padding:4px 0; color:#888; font-size:15px; text-align:center;  }
    .tbl a{ text-decoration:none; color:green; font-size:15px; vertical-align:middle; }
    .tbl a:hover{text-decoration:underline;}
    .tbl td{ border-bottom:1px solid #222222; padding:0 8px; line-height:24px;  vertical-align:middle; width: 300px; }
    .tbl th{ padding:3px 8px; font-weight:normal; background:#222222; color:#555; vertical-align:middle; }
    .tbl td:hover{ background:#181818; }
    #alert {position: relative;}
    #alert:hover:after {background: hsla(0,0%,0%,.8);border-radius: 3px;color: #f6f6f6;content: 'Click to dismiss';font: bold 12px/30px sans-serif;height: 30px;left: 50%;margin-left: -60px;position: absolute;text-align: center;top: 50px; width: 120px;}
    #alert:hover:before {border-bottom: 10px solid hsla(0,0%,0%,.8);border-left: 10px solid transparent;border-right: 10px solid transparent;content: '';height: 0;left: 50%;margin-left: -10px;position: absolute;top: 40px;width: 0;}
    #alert:target {display: none;}
    .alert_red {animation: alert 1s ease forwards;background-color: #c4453c;background-image: linear-gradient(135deg, transparent,transparent 25%, hsla(0,0%,0%,.1) 25%,hsla(0,0%,0%,.1) 50%, transparent 50%,transparent 75%, hsla(0,0%,0%,.1) 75%,hsla(0,0%,0%,.1));background-size: 20px 20px;box-shadow: 0 5px 0 hsla(0,0%,0%,.1);color: #f6f6f6;display: block;font: bold 16px/40px sans-serif;height: 40px;position: absolute;text-align: center;text-decoration: none;top: -45px;width: 100%;}
    .alert_green {animation: alert 1s ease forwards;background-color: #43CD80;background-image: linear-gradient(135deg, transparent,transparent 25%, hsla(0,0%,0%,.1) 25%,hsla(0,0%,0%,.1) 50%, transparent 50%,transparent 75%, hsla(0,0%,0%,.1) 75%,hsla(0,0%,0%,.1));background-size: 20px 20px;box-shadow: 0 5px 0 hsla(0,0%,0%,.1);color: #f6f6f6;display: block;font: bold 16px/40px sans-serif;height: 40px;position: absolute;text-align: center;text-decoration: none;top: -45px;width: 100%;}
    @keyframes alert {0% { opacity: 0; }50% { opacity: 1; }100% { top: 0; }}
    #divAlert { background-color:green; color:white;}
    </style>
</head>
<body>






<?php
@error_reporting(4);
$user = $_COOKIE['ftpuser'];
$passwd = !$_COOKIE['ftppassword']?"":$_COOKIE['ftppassword'];
$host = $_COOKIE['host'];
if($user=="")
{
	if(isset($_GET['host']) && isset($_GET['usrname']) && isset($_GET['passwrd']))
	{
		global $user, $passwd, $host;
		setcookie("ftpuser", $_GET['usrname']);
		setcookie("ftppassword", $_GET['passwrd']);
		setcookie("host", $_GET['host']);
		print "<script>window.location='?'</script>";
		$user = $_COOKIE['ftpuser'];
		$passwd = !$_COOKIE['ftppassword']?"":$_COOKIE['ftppassword'];
		$host = $_COOKIE['host'];		
	}
	else {
		echo '<title>INDRAJITH FTP MNAGER v.1.0</title><center>
            <div id=loginbox><p><font face="verdana,arial" size=-1>
            <font color=orange>>>>>>>>>>></font><font color=white>>>>>><<<<<</font><font color=green>>>>>>>>>>></font>
            <center><table cellpadding=\'2\' cellspacing=\'0\' border=\'0\' id=\'ap_table\'>
            <tr><td bgcolor="green"><table cellpadding=\'0\' cellspacing=\'0\' border=\'0\' width=\'100%\'><tr><td bgcolor="green" align=center style="padding:2;padding-bottom:4"><b><font color="white" size=-1 color="white" face="verdana,arial"><b>INDRAJITH FTP MANAGER</b></font></th></tr>
            <tr><td bgcolor="black" style="padding:5">
            <form method="GET">
            <input type="hidden" name="action" value="login">
            <input type="hidden" name="hide" value="">
            <center><table>
            <tr><td><font color="green" face="verdana,arial" size=-1>Host:</font></td><td><input type="text" size="30" name="host" value="localhost" onfocus="if (this.value == \'localhost\'){this.value = \'\';}"></td></tr>
            <tr><td><font color="green" face="verdana,arial" size=-1>Login:</font></td><td><input type="text" size="30" name="usrname" value="username" onfocus="if (this.value == \'username\'){this.value = \'\';}"></td></tr>
            <tr><td><font color="green" face="verdana,arial" size=-1>Password:</font></td><td><input type="password" size="30" name="passwrd" value="password" onfocus="if (this.value == \'password\') this.value = \'\';"></td></tr>
            <tr><td><font face="verdana,arial" size=-1>&nbsp;</font></td><td><font face="verdana,arial" size=-1><input type="submit" value="Enter"></font></td></tr></table>
            </div><br /></center>';
			exit;
	}
}

?>


<?php

urldecode($str);
$self=$_SERVER['PHP_SELF'];
$srvr_sof=$_SERVER['SERVER_SOFTWARE'];
$your_ip=$_SERVER['REMOTE_ADDR'];
$srvr_ip=$_SERVER['SERVER_ADDR'];
$admin=$_SERVER['SERVER_ADMIN'];

if(strtolower(substr(PHP_OS,0,3))=="win")
{
    $os="win";
    $sep="\\";
    $ox="Windows";
}
else
{
    $os="nix";
    $ox="Linux";
}

$user = $_COOKIE['ftpuser'];
$passwd = $_COOKIE['ftppassword'];
$host = str_replace("ftp://", "", $_COOKIE['host']);
$connect = ftp_connect($host);
if(!$connect)
{
	print '<script>alert("Error: Host connection failed!!!");</script>';
	exit(1);
}
$login = ftp_login($connect, $user, $passwd);
if(!$login)
{
	print '<script>alert("Error: Login failed!!!");</script>';
	exit(1);
}


echo "<title>INDRAJITH FTP MANAGER v.1.0</title><div id=result>
<table>
    <tbody>
        <tr>
            <td style='border-right:1px solid #104E8B;' width=\"300px;\">
            <div style='text-align:center;'>
                <a href='?' style='text-decoration:none;'><h1>INDRAJITH</h1></a><font color=blue>FTP MANAGER</font>
            </div>
            </td>
            <td>
            <div class=\"header\">OS</font> <font color=\"#666\" >:</font>
            ".$ox." </font> <font color=\"#666\" >|</font> ".php_uname()."<br />Host: <font color=red><a style='color:red;' href='ftp://".$host."'>".$host."</a></font><br />
            Your IP : <font color=red>".$your_ip."</font> <font color=\"#666\" >|</font> Server IP : <font color=red>".$srvr_ip."</font> <font color=\"#666\" > | </font> Admin <font color=\"#666\" > : </font> <font color=red> {$admin} </font> <br />FTP OS: <font color=red>".ftp_systype($connect)."</font> | Time Out: <font color=red>".ftp_get_option($connect, FTP_TIMEOUT_SEC)." Sec</font> <br />
            <a style='color:red; size:10px; ' href='?logout'>Logout</a></font><br />";
            echo "
            </div>
            </td>
        </tr>
    </tbody>
</table></div>";

$fpath = ftp_pwd($connect);
if(isset($_GET['fpath']))
{
	$fpath=$_GET['fpath'];
	ftp_chdir($connect, $fpath);
}

function success($str)
{	
	echo "<div id='alert'><a class=\"alert_green\" href=\"#alert\">".$str."</a></div>";
	ftp_man_bg();
}

function failed($str)
{
	echo "<div id='alert'><a class=\"alert_red\" href=\"#alert\">".$str."</a></div>";
	ftp_man_bg();
}

function filesizex($size)
{
    if ($size>=1073741824)$size = round(($size/1073741824) ,2)." GB";
    elseif ($size>=1048576)$size = round(($size/1048576),2)." MB";
    elseif ($size>=1024)$size = round(($size/1024),2)." KB";
    else $size .= " B";
    return $size;
}

function dirDetails($directory=".")
{
	global $connect;
	if(is_array($directs=ftp_rawlist($connect, $directory)))
	{
		$prop = array();
		$nl=count($directs);
		foreach($directs as $dirx)
		{
			$chunks = preg_split("/[\s]+/", $dirx, 9);
			list($prop['perm'], $prop['num'], $prop['user'], $prop['group'], $prop['size'], $prop['mon'], $prop['day'], $prop['time']) = $chunks;
			$prop['type']=$chunks[0]{0}==='d'?'Directory':'File';
			$prop['name']=$chunks[8];
			array_splice($chunks, 0, 8);
			$props[implode(" ", $chunks)]=$prop;
		}
		return $props;
	}
}

function deleteFiled($file, $dire)
{
	global $connect;
	ftp_chdir($connect, $dire);
	$del = ftp_delete($connect, $file);
	if($del)
	{
		success("The file $file deleted sucessfully");
	}
	else {
		failed("The file $file cannot delete");
	}
}

function deleteFiledd($file, $dire)
{
	global $connect;
	ftp_chdir($connect, $dire);
	$del = ftp_delete($connect, $file);
}


function download($resource, $fname)
{
	$down = ftp_get($resource, $fname, $fname, FTP_ASCII);
	if($down)
	{
		success("The file ".$fname." downloaded sucessfully");
	}
	else
	{
		failed("The file ".$fname." failed to download");
	}
}

function renamef($resource, $fname, $nfname)
{
	$ren = ftp_rename($resource, $fname, $nfname);
	if($ren)
	{
		success("The file ".$fname." renamed sucessfully");
	}
	else
	{
		failed("The file ".$fname." failed to rename");
	}
}

function rename_ui($path)
{
    $rf_path=$_GET['rename'];
    echo "<div id=result><center><h2>Rename</h2><hr /><p><br /><br /><form method='GET'><input type=hidden name=fpath value='$path'><input type=hidden name='old_name' size='40' value='".$rf_path."'>New Name : <input name='new_name' size='40' value='".basename($rf_path)."'><input type='submit' value='   >>>   ' /></form></p><br /><br /><hr /><br /><br /></center></div>";
}

function changeperm_ui($path)
{
	$rf_path=$_GET['chperm'];
    echo "<div id=result><center><h2>New Permission</h2><hr /><p><br /><br /><form method='GET'><input type=hidden name=fname value='$rf_path'><input type=hidden name=fpath value='$path'>New Permission : <input name='new_perm' size='40'><input type='submit' value='   >>>   ' /></form></p><br /><br /><hr /><br /><br /></center></div>";
	
}

function changeperm()
{
	global $connect, $fpath;
	ftp_chdir($connect, $fpath);
	$mode = $_GET['new_perm'];
	$file = $_GET['fname'];
	$mode = octdec(str_pad($mode, 4, '0', STR_PAD_LEFT));
	if(ftp_chmod($connect, $mode, $file) != FALSE)
		{
			success("The file $file permission changed successfully");
		}
		else 
		{
			failed("The file $file permission cannot changed");
		}
}

function uploadf()
{
	$file=$_GET['upload'];
	global $connect, $host;
	$uploadd = ftp_put($connect, $file, $file, FTP_ASCII);
	if($uploadd)
	{
		success("Successfully uploaded file $file to FTP Server $host");
	}
	else 
	{
		failed("Failed to upload file $file to FTP Server $host");
	}
}

function deleteDir($file, $cdir)
{
	global $connect, $fpath;
	$arraay = dirDetails("$fpath/$file"); //ftp_nlist($connect, "$cdir/$file");
	ftp_rmdir($connect, $file);
	foreach ($arraay as $key)
	{
		if($key['type']!='Directory')
		{
			deleteFiledd($fpath."/".$file."/".$key['name']);
		}
		else
		{
			deleteDir($fpath."/".$file."/".$key['name']);
		}
	}
	if(ftp_rmdir($connect, $file))
	{
		success("The directory $file has deleted succssfully");
	}
	else 
	{
		failed("The directory $file cannot delete");
	}
}

function createDir($file, $dir)
{
	global $connect;
	if(ftp_mkdir($connect, $dir))
	{
		success("The directory $dir created successfully");
	}
	else 
	{
		failed("The directory $dir failed to create");
	}
}

function logout()
{
	setcookie("ftpuser", "");
	setcookie("ftppassword", "");
	setcookie("host", "");
	print "<script>window.location='?';</script>";
}
?>



<?php
if(isset($_REQUEST["fopen"]))
{
	global $connect;
	$fname = $_REQUEST["fopen"];
	download($connect, $fname);
}

else if(isset($_GET['fpath']) && isset($_GET['rename']))
{
	rename_ui($_GET['fpath']);
}

else if(isset($_GET['old_name']) && isset($_GET['new_name']) && isset($_GET['fpath']))
{
	renamef($connect, $_REQUEST['old_name'], $_REQUEST['new_name']);
}

elseif (isset($_GET['delete']) && isset($_GET['fpath'])) 
{
	deleteFiled($_GET['delete'], $_GET['fpath']);
}

else if(isset($_REQUEST['logout']))
{
	logout();
}

elseif (isset($_GET['deleteDir']) && isset($_GET['fpath'])) 
{
	deleteDir($_GET['deleteDir'], $_GET['fapth']);
}

else if(isset($_GET['chperm']) && isset($_GET['fpath']))
{
	changeperm_ui($_GET['fpath']);
}

else if(isset($_GET['fname']) && isset($_GET['fpath']) && isset($_GET['new_perm']))
{
	changeperm();
}

else if(isset($_GET['fpath']) && isset($_GET['upload']))
{
	uploadf();
}

else if(isset($_GET['fpath']) && isset($_GET['createDir']))
{
	createDir($_GET['fpath'], $_GET['createDir']);
}

else if(isset($_GET['cmd']) && isset($_GET['fpath']))
{
	if(ftp_exec($connect, $_GET['cmd']))
	{
		success("The command ".$_GET['cmd']." executed successfully!!!");
	}
	else {
		failed("The command ".$_GET['cmd']." failed to execute!!!");
	}
}

else
{
	ftp_man_bg();
}
function ftp_man_bg()
{
	global $fpath, $connect, $host;
	$path=!empty($_GET['path'])?$_GET['path']:getcwd();
    $dirs=array();
    $fils=array();
    if(is_dir($path))
    {
        chdir($path);
        if($handle=opendir($path))
        {
            while(($item=readdir($handle))!==FALSE)
            {
                if($item=="."){continue;}
                if($item==".."){continue;}
                else
                {
                    array_push($fils, $item);
                }
            }
        }
	}
	echo "<div id = resultx><br /><center><form method='GET'>pwd: <input name=fpath size=80 value='".ftp_pwd($connect)."'><input type=submit value='   >>   '></form>
	<form method='GET'> <input name=fpath type=hidden size=80 value='".ftp_pwd($connect)."'>Upload File: <select name='upload' style='color:green; background-color:black; border:1px solid #666;'>";
                      foreach($fils as $fil)
                      {
                      	print "<option>$fil</option>";
                      }
    print "</select><input type=submit value='   >>   '></form><table class=tbl><tr><td><form method=GET><input name=fpath value='$fpath' type=hidden>Create Directory: <input type=submit value='   >>   '><input name=createDir size=65></form></td><td><form method=GET>CMD: <input name=cmd size=65><input name=fpath value='$fpath' type=hidden><input type=submit value='   >>   '></td></tr></table></form></div>";
	echo "<div id=result><table class=table>
    <tr>
    <th width='300px'>Name</th>
	<th width='100px'>Type</th>
    <th width='100px'>Size</th>
    <th width='150px'>Last Modified</th>
    <th width='100px'>Permissions</th>
	<th width='100px'>User</th>
	<th width='100px'>Group</th>
	<th width='300px'>Actions</th>
    </tr>";
	$property = dirDetails($fpath);
	foreach($property as $prop)
	{
		global $fpath;
		$modified = date("F d Y",ftp_mdtm($connect, $prop['name']));
		if($prop['type']=='Directory')
		{
			print "<tr><td><a href='?fpath=".$fpath."/".$prop["name"]."'>[ ".$prop["name"]." ]</a></td><td>".$prop['type']."</td><td>".filesizex($prop['size'])."</td><td>$modified</td><td><a href='?fpath=$fpath&amp;chperm=".$prop['name']."'>".$prop['perm']."</a></td><td>".$prop['user']."</td><td>".$prop['group']."</td><td><a href='?fpath=$fpath&amp;rename=".$prop['name']."'>Rename</a> | <a href='?fpath=$fpath&amp;deleteDir=".$prop['name']."'>Delete</a></td></tr>";
		}
		else
		{
			print "<tr><td><a href='?fpath=$fpath&amp;fopen=".$prop['name']."'>".$prop['name']."</a></td><td>".$prop['type']."</td><td>".filesizex($prop['size'])."</td><td>$modified</td><td><a href='?fpath=$fpath&amp;chperm=".$prop['name']."'>".$prop['perm']."</a></td><td>".$prop['user']."</td><td>".$prop['group']."</td><td><a href='?fpath=$fpath&amp;fopen=".$prop['name']."'>Download</a> | <a href='?fpath=$fpath&amp;rename=".$prop['name']."'>Rename</a> | <a href='?fpath=$fpath&amp;delete=".$prop['name']."'>Delete</a></td></tr>";
		}
	}
}

?>
</table></div><div id=result><center></p><p><font size=4 color=green>&copy <a style='color:green; text-decoration:none;' href=http://facebook.com/ajithkp560>AJITH KP</a> & <a style='color:green; text-decoration:none;' href='http://www.facebook.com/vishnunathkp'>VISHNU NATH KP</a> &copy</font><br />&copy<a href='http://www.terminalcoders.blogspot.in'> _TERMINAL_CODERS_ [2012] </a>&copy</center></div>
</body>
</html>
